package com.mes.cloud.service.productpricegroup;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.api.dto.request.productpricegroup.ProductPriceGroupCustomerGroupBatchDeleteRequest;
import com.mes.cloud.api.dto.request.productpricegroup.ProductPriceGroupCustomerGroupBindCustomerGroupRequest;
import com.mes.cloud.api.dto.request.productpricegroup.ProductPriceGroupCustomerGroupListRequest;
import com.mes.cloud.api.dto.request.productpricegroup.ProductPriceGroupCustomerGroupPageListRequest;
import com.mes.cloud.api.dto.response.productpricegroup.ProductPriceGroupCustomerGroupCustomerGroupDto;
import com.mes.cloud.api.dto.response.productpricegroup.ProductPriceGroupCustomerGroupDto;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.ProductPriceGroupCustomerGroupDao;
import com.mes.cloud.entity.ProductPriceGroupCustomerGroup;
import com.mes.cloud.entity.vo.productpricegroup.ProductPriceGroupCustomerGroupCustomerGroupVo;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.service.IPKeyGenerator;
import com.mes.cloud.util.CommonUtil;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 产品价格组-产品客户组关联
 *
 * @author wxm
 * @version 2019-09-24 14:49:07
 * @email xiaomin.wu1@pactera.com
 */
@Service
public class ProductPriceGroupCustomerGroupService {

    private ProductPriceGroupCustomerGroupDao productPriceGroupCustomerGroupDao;

    private IPKeyGenerator iPKeyGenerator;

    public ProductPriceGroupCustomerGroupService(ProductPriceGroupCustomerGroupDao productPriceGroupCustomerGroupDao, IPKeyGenerator iPKeyGenerator) {
        this.productPriceGroupCustomerGroupDao = productPriceGroupCustomerGroupDao;
        this.iPKeyGenerator = iPKeyGenerator;
    }

    /**
     * 分页查列表
     *
     * @param productPriceGroupCustomerGroupPageListRequest
     * @return JsonResult
     */
    public JsonResult<CommPager<ProductPriceGroupCustomerGroupCustomerGroupDto>> pageList(ProductPriceGroupCustomerGroupPageListRequest productPriceGroupCustomerGroupPageListRequest) {
        Page<ProductPriceGroupCustomerGroupDto> page = PageHelper.startPage(productPriceGroupCustomerGroupPageListRequest.getCurrentPage(), productPriceGroupCustomerGroupPageListRequest.getPageSize());
        //查询条件
        ProductPriceGroupCustomerGroupListRequest productPriceGroupCustomerGroupListRequest = new ProductPriceGroupCustomerGroupListRequest();
        BeanUtils.copyProperties(productPriceGroupCustomerGroupPageListRequest, productPriceGroupCustomerGroupListRequest);
        List<ProductPriceGroupCustomerGroupCustomerGroupVo> productPriceGroupCustomerGroupCustomerGroupVoList = productPriceGroupCustomerGroupDao.list(productPriceGroupCustomerGroupListRequest);
        List<ProductPriceGroupCustomerGroupCustomerGroupDto> productPriceGroupCustomerGroupCustomerGroupDtoList = new ArrayList<>();
        for (ProductPriceGroupCustomerGroupCustomerGroupVo productPriceGroupCustomerGroupCustomerGroupVo : productPriceGroupCustomerGroupCustomerGroupVoList) {
            ProductPriceGroupCustomerGroupCustomerGroupDto productPriceGroupCustomerGroupCustomerGroupDto = new ProductPriceGroupCustomerGroupCustomerGroupDto();
            BeanUtils.copyProperties(productPriceGroupCustomerGroupCustomerGroupVo, productPriceGroupCustomerGroupCustomerGroupDto);
            productPriceGroupCustomerGroupCustomerGroupDtoList.add(productPriceGroupCustomerGroupCustomerGroupDto);
        }
        CommPager<ProductPriceGroupCustomerGroupCustomerGroupDto> pageResult = new CommPager<>(page, productPriceGroupCustomerGroupCustomerGroupDtoList);
        JsonResult<CommPager<ProductPriceGroupCustomerGroupCustomerGroupDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);
        return jsonResult;
    }

    /**
     * 绑定客户组
     * 更新产品价格组客户数
     *
     * @param productPriceGroupCustomerGroupBindCustomerGroupRequest
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult bindCustomerGroup(ProductPriceGroupCustomerGroupBindCustomerGroupRequest productPriceGroupCustomerGroupBindCustomerGroupRequest) {
        JsonResult jsonResult = new JsonResult();
        //获取价格组已有的客户组
        ProductPriceGroupCustomerGroup productPriceGroupCustomerGroupParam = new ProductPriceGroupCustomerGroup();
        productPriceGroupCustomerGroupParam.setProductPriceGroupId(productPriceGroupCustomerGroupBindCustomerGroupRequest.getProductPriceGroupId());
        //未删除状态
        productPriceGroupCustomerGroupParam.setDelFlag(Constants.DEL_FLAG_NO);
        List<ProductPriceGroupCustomerGroup> existProductPriceGroupCustomerGroupList = productPriceGroupCustomerGroupDao.select(productPriceGroupCustomerGroupParam);

        //新增客户组剔除数据库已有客户组
        //客户组已有客户组列表     前端传递的客户组列表
        //两个列表做比对，已有客户组不需要新增
        Set<String> idList = deleteExistCustomerGroupId(existProductPriceGroupCustomerGroupList, productPriceGroupCustomerGroupBindCustomerGroupRequest.getIds());
        List<ProductPriceGroupCustomerGroup> productPriceGroupCustomerGroupList = generatorProductPriceGroupCustomerGroup(idList, productPriceGroupCustomerGroupBindCustomerGroupRequest.getProductPriceGroupId());
        if (CollectionUtils.isEmpty(productPriceGroupCustomerGroupList)) {
            return jsonResult.setCode(HttpStatusEnum.REF_EXIST.getCode()).setMsg(HttpStatusEnum.REF_EXIST.getMessage());
        }
        //批量插入
        productPriceGroupCustomerGroupDao.batchInsert(productPriceGroupCustomerGroupList);
        return jsonResult;
    }

    /**
     * 封装关联表
     *
     * @param idList              客户组ID
     * @param productPriceGroupId 产品价格组ID
     * @return
     */
    private List<ProductPriceGroupCustomerGroup> generatorProductPriceGroupCustomerGroup(Set<String> idList, String productPriceGroupId) {
        List<ProductPriceGroupCustomerGroup> productPriceGroupCustomerGroupList = new ArrayList<ProductPriceGroupCustomerGroup>();
        if (CollectionUtils.isEmpty(idList)) {
            return productPriceGroupCustomerGroupList;
        }
        ProductPriceGroupCustomerGroup productPriceGroupCustomerGroupTemp = null;
        for (String id : idList) {
            productPriceGroupCustomerGroupTemp = generatorProductPriceGroupCustomerGroup(productPriceGroupId, id);
            productPriceGroupCustomerGroupList.add(productPriceGroupCustomerGroupTemp);
        }
        return productPriceGroupCustomerGroupList;
    }

    /**
     * 封装一个productPriceGroupCustomerGroup
     *
     * @param productPriceGroupId
     * @param customerGroupId
     * @return
     */
    private ProductPriceGroupCustomerGroup generatorProductPriceGroupCustomerGroup(String productPriceGroupId, String customerGroupId) {
        ProductPriceGroupCustomerGroup productPriceGroupCustomerGroupTemp = new ProductPriceGroupCustomerGroup();
        productPriceGroupCustomerGroupTemp.setId(iPKeyGenerator.getGenerateKey());
        productPriceGroupCustomerGroupTemp.setProductPriceGroupId(productPriceGroupId);
        productPriceGroupCustomerGroupTemp.setProductCustomerGroupId(customerGroupId);
        productPriceGroupCustomerGroupTemp.setUpdateDateStr(DateUtil.updateDateStr());
        productPriceGroupCustomerGroupTemp.setCreateBy(UserUtil.getCreateByName());
        productPriceGroupCustomerGroupTemp.setUpdateBy(UserUtil.getUpdateByName());
        productPriceGroupCustomerGroupTemp.setCreateByNo(UserUtil.getCreateByNo());
        productPriceGroupCustomerGroupTemp.setUpdateByNo(UserUtil.getUpdateByNo());
        return productPriceGroupCustomerGroupTemp;
    }

    /**
     * 两个客户列表做对比，客户列表中已经存在的ID剔除
     *
     * @param existProductPriceGroupCustomerGroupList 数据库中又有的客户列表
     * @param ids                                     前端传递的客户列表
     * @return
     */
    private Set<String> deleteExistCustomerGroupId(List<ProductPriceGroupCustomerGroup> existProductPriceGroupCustomerGroupList, Set<String> ids) {
        if (CollectionUtils.isEmpty(existProductPriceGroupCustomerGroupList)) {
            return ids;
        }
        Set<String> existIdList = new HashSet<>();
        //价格组已经拥有的客户组ID存放到existIdList
        for (ProductPriceGroupCustomerGroup productPriceGroupCustomerGroup : existProductPriceGroupCustomerGroupList) {
            existIdList.add(productPriceGroupCustomerGroup.getProductCustomerGroupId());
        }
        return CommonUtil.deleteExistId(existIdList, ids);
    }

    /**
     * 批量解绑
     *
     * @param productPriceGroupCustomerGroupBatchDeleteRequest
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult batchDelete(ProductPriceGroupCustomerGroupBatchDeleteRequest productPriceGroupCustomerGroupBatchDeleteRequest) {
        JsonResult jsonResult = new JsonResult();
        productPriceGroupCustomerGroupDao.batchDelete(productPriceGroupCustomerGroupBatchDeleteRequest.getIds(),
                DateUtil.updateDateStr(), UserUtil.getEmployeeNo(), UserUtil.getName());
        return jsonResult;
    }
}
